<?php
include_once('libs/class.database.php');

class Notif_laku
{
	public $link;
	
	function __construct()
	{
		$konek= new Database();
		$this->link=$konek->connect();
	}
	
	function createNotif($id_penjual, $id_pembeli)
	{
		$date_created=date('Y-m-d');
		$query=$this->link->prepare("INSERT INTO notif_laku(id_penjual, id_pembeli, date_created) 
			 VALUES(?,?,?)");
		$data=array($id_penjual, $id_pembeli, $date_created);
		$query->execute($data);
		$jml_row=$query->rowCount();
		return $jml_row;
	}
	
	function getNotif($id_penjual)
	{
		$query=$this->link->query("SELECT a.id id,a.id_penjual id_penjual,a.id_pembeli id_pembeli,a.date_created tgl_beli,a.status status,b.nama nama FROM notif_laku a 
									LEFT JOIN biodata b on a.id_pembeli=b.id_username WHERE a.id_penjual='$id_penjual' AND a.status=0 GROUP BY a.id_penjual,a.id_pembeli,a.date_created");
		$jml_data=$query->rowCount();
		if($jml_data!=0)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getJmlNotif($id_penjual)
	{
		$query=$this->link->query("SELECT count(id) jumlah FROM notif_laku WHERE id_penjual='$id_penjual' AND status=0 GROUP BY id_penjual");
		$jml_data=$query->rowCount();
		if($jml_data!=0)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function updateNotif($id)
	{
		$date_updated=date('Y-m-d');
		$query=$this->link->query("UPDATE notif_laku SET status=1, date_updated='$date_updated' WHERE id='$id' AND status=0");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
}